#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QDir>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_pMusicPlayer(nullptr)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    if(m_pMusicPlayer)
    {
        m_pMusicPlayer->DeInit();
        delete m_pMusicPlayer;
        m_pMusicPlayer = nullptr;
    }
    delete ui;
}

void MainWindow::Init()
{
    setWindowTitle("MusicPlayer");

    m_listTime[0] =ui->timeEdit1->time();
    m_listTime[1] =ui->timeEdit2->time();
    m_listTime[2] =ui->timeEdit3->time();

    m_pMusicPlayer = new Player();
    m_pMusicPlayer->Init();

    m_timer.setSingleShot(false);
    connect(&m_timer, &QTimer::timeout, this, &MainWindow::Timeout);

    m_timer.start(60*1000);
}

void MainWindow::Timeout()
{
    int hour = QTime::currentTime().hour();
    int min = QTime::currentTime().minute();

    for(QTime& t : m_listTime)
    {
        if( t.hour() == hour && t.minute() == min)
        {
            PlayNextMusic();
        }
    }
}

void MainWindow::PlayNextMusic()
{
    static int nOldIndex = -1;
    if(m_listFile.size()>0)
    {
        qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
        int nIndex = rand()% m_listFile.size();
        qDebug() << "m_listFile.size()" << m_listFile.size();
        while(nIndex == nOldIndex)
        {
            nIndex = rand()% m_listFile.size();
            qDebug() <<"reRand:"<< nIndex;
        }
        qDebug() << "nIndex = " << nIndex;
        if(m_pMusicPlayer->Play(m_sPlayDir + "\\" + m_listFile.at(nIndex)))
        {
            QString sTip = "[Play] ";
            ui->label_playing->setText( sTip+ m_listFile.at(nIndex));
        }
        nOldIndex = nIndex;
    }
}

void MainWindow::on_BrowerButton_clicked()
{
    QString dirPath = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    QDir dir(dirPath);
    dirPath.replace(QString("/"), QString("\\"));

    QStringList stringlist;
    stringlist.append("*.mp3");
    dir.setNameFilters(stringlist);

    if(dirPath.size()>0)
    {
        m_sPlayDir = dirPath;
        this->ui->label_path->setText(dirPath);
        m_listFile.clear();

        QFileInfoList list = dir.entryInfoList();
        for (QFileInfo& fileInfo : list) {
            //qDebug ("%s" ,fileInfo.fileName();
            qDebug()<<fileInfo.fileName();
            m_listFile.append(fileInfo.fileName());
        }

        PlayNextMusic();
    }
}

void MainWindow::on_timeEdit1_timeChanged(const QTime &time)
{
    m_listTime[0] = time;
    qDebug()<< "[timeEdit1]" << time.hour() << ":" << time.minute();
}

void MainWindow::on_timeEdit2_timeChanged(const QTime &time)
{
    m_listTime[1] = time;
    qDebug()<< "[timeEdit2]" << time.hour() << ":" << time.minute();
}

void MainWindow::on_timeEdit3_timeChanged(const QTime &time)
{
    m_listTime[2] = time;
    qDebug()<< "[timeEdit3]" << time.hour() << ":" << time.minute();
}
